package com.flute.tools.util.collection;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;

public class InstanceAccumulator implements Serializable {
	private static final long serialVersionUID = 9056230844665828614L;
	private HashMap<Object, Integer> map;
	private int allCount;

	public InstanceAccumulator() {
		this.map = new HashMap<Object, Integer>();
	}

	public void addKey(Object object) {
		if (!map.containsKey(object)) {
			map.put(object, 0);
		}

		map.put(object, map.get(object) + 1);
		allCount++;
	}

	public void addKey(Object object, int count) {
		if (!map.containsKey(object)) {
			map.put(object, 0);
		}

		map.put(object, map.get(object) + count);
		allCount = allCount + count;
	}

	public void minusKey(Object object) {
		if (map.containsKey(object)) {
			map.put(object, map.get(object) - 1);
			allCount--;
		}
	}

	public boolean containsKey(Object object) {
		return map.containsKey(object);
	}

	public Set<Object> getKeys() {
		return map.keySet();
	}

	public void clear() {
		this.map.clear();
		allCount = 0;
	}

	public void clear(Object object) {
		allCount = allCount - map.get(object);
		this.map.put(object, 0);
	}

	public void delete(Object object) {
		allCount = allCount - map.get(object);
		this.map.remove(object);
	}

	public int getCount(Object object) {
		if (map.containsKey(object)) {
			return map.get(object);
		} else {
			return 0;
		}
	}

	public int getAllCount() {
		return allCount;
	}

	@SuppressWarnings("unchecked")
	public InstanceAccumulator clone() {
		InstanceAccumulator clone = new InstanceAccumulator();
		clone.map = (HashMap<Object, Integer>) map.clone();

		return clone;
	}

	public String toString() {
		return this.getClass().getSimpleName() + "[" + this.map + "]";
	}

}
